﻿using System;
using CrazyTalk.Common;

namespace CrazyTalk.Config
{
    class SecurityConfigFileManager : DefaultConfigFileManager
    {
        private static SecurityInfo _mConfiginfo;

        /// <summary>
        /// 文件修改时间
        /// </summary>
        private static DateTime _mFileoldchange;


        /// <summary>
        /// 初始化文件修改时间和对象实例
        /// </summary>
        static SecurityConfigFileManager()
        {
            _mFileoldchange = System.IO.File.GetLastWriteTime(ConfigFilePath);
            _mConfiginfo = (SecurityInfo)DeserializeInfo(ConfigFilePath, typeof(SecurityInfo));
        }

        /// <summary>
        /// 当前的配置类实例
        /// </summary>
        public new static IConfigInfo ConfigInfo
        {
            get { return _mConfiginfo; }
            set { _mConfiginfo = (SecurityInfo)value; }
        }

        /// <summary>
        /// 配置文件所在路径
        /// </summary>
        public static string Filename = null;


        /// <summary>
        /// 获取配置文件所在路径
        /// </summary>
        public new static string ConfigFilePath
        {
            get
            {
                if (Filename == null)
                {
                    string ss = BaseConfigs.GetForumPath;
                    Filename = Utils.GetMapPath(BaseConfigs.GetForumPath + "Config/Security.config");
                }

                return Filename;
            }
        }

        /// <summary>
        /// 返回配置类实例
        /// </summary>
        /// <returns></returns>
        public static SecurityInfo LoadConfig()
        {
            //先判断为空,再进行加载
            if (_mConfiginfo == null)
            {
                ConfigInfo = LoadConfig(ref _mFileoldchange, ConfigFilePath, ConfigInfo);
            }
            return ConfigInfo as SecurityInfo;
        }

        /// <summary>
        /// 保存配置类实例
        /// </summary>
        /// <returns></returns>
        public override bool SaveConfig()
        {
            return SaveConfig(ConfigFilePath, ConfigInfo);
        }
    }
}
